package pers.linkong.asr.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pers.linkong.asr.domain.RecordBean;
import pers.linkong.asr.domain.RecordNumBean;
import pers.linkong.asr.mapper.RecordMapper;

@Service
// Service组件
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, readOnly = false)
public class RecordService {
	@Autowired
	private RecordMapper recordMapper;

	// 增
	public void addRecord(RecordBean record) throws Exception {
		this.recordMapper.addRecord(record);
		System.out.println("添加记录：" + record.getSid());
		// throw new RuntimeException();
	}

	// 删
	public void deleteRecord(int rid) throws Exception {
		this.recordMapper.deleteRecord(rid);
		System.out.println("删除记录：" + rid);
	}

	// 查
	public RecordBean getRecordById(int rid) throws Exception {
		RecordBean record = recordMapper.getRecordById(rid);
		System.out.println("查询记录：" + record.getRid());
		return record;
	}
	
	// 根据图片编号查学生学号
	public int getSidByImgId(int imgid) throws Exception {
		return this.recordMapper.getSidByImgId(imgid);
	};

	// 改
	public void updateRecord(RecordBean record) throws Exception {
		this.recordMapper.updateRecord(record);
		System.out.println("修改记录：" + record.getRid());
	}

	// 分页
	public List<RecordBean> findRecordByPage(RecordBean record, long start,
			long limit) throws Exception {
		List<RecordBean> recordBeans = this.recordMapper.findRecordByPage(record, start, limit);
		return recordBeans;
	}
	
	// 检测页分页
	public List<RecordBean> findDetRecByPage(int cidSublevel, String classid, long start,
			long limit) throws Exception {
		List<RecordBean> recordBeans = this.recordMapper.findDetRecByPage(cidSublevel, classid, start, limit);
		return recordBeans;
	}
	
	// 统计
	public long getRecordCount(RecordBean record) throws Exception {
		return this.recordMapper.getRecordCount(record);
	}
	
	// 主页统计
	public long getDetRecCount(String classid) throws Exception {
		return this.recordMapper.getDetRecCount(classid);
	}
	
	// 查找这个学生本次课有无签到（已到，迟到）记录
	public long getRidCountByRecord(RecordBean record) {
		return this.recordMapper.getRidCountByRecord(record);
	}
	
	// 查找这个学生本次课有无签到（早退）记录
	public long getRidCountByRecord2(RecordBean record) {
		return this.recordMapper.getRidCountByRecord2(record);
	}
	
	// 查询这个学生是否上这门课，是返回1，否返回0
	public long getStudentCountByCsid(int sid, int csid) throws Exception {
		return this.recordMapper.getStudentCountByCsid(sid, csid);
	}
	
	// 查找复合当前时间的记录中每种flag的个数
	public RecordNumBean getRecordNum(int cidSublevel, String classid) throws Exception {
		return this.recordMapper.getRecordNum(cidSublevel, classid);
	}
	
	// 签离功能
	public void updateDetRecord(int cidSublevel, int flag){
		this.recordMapper.updateDetRecord(cidSublevel, flag);
		System.out.println("签离启动:" + cidSublevel);
	}

	public void updateRecordByZaotui(RecordBean record) {
		this.recordMapper.updateRecordByZaotui(record);
		System.out.println("签离记录：" + record.getSid());
	}
}
